mui.init();
mui.init({
	pullRefresh: {
		container: "#refreshContainer", //待刷新区域标识，querySelector能定位的css选择器均可，比如：id、.class等
		up: {
			height: 50, // 可选.默认50.触发上拉加载拖动距离
			auto: true, // 可选,默认false.自动上拉加载一次
			contentrefresh: "正在加载...", // 可选，正在加载状态时，上拉加载控件上显示的标题内容
			contentnomore: '没有更多的数据了', // 可选，请求完毕若没有更多数据时显示的提醒内容；
			callback: pullupRefresh, //加载数据方法 
			contentinit: '',
			 contentdown: ''
		}
	}
})
var pagenum = 1; //页数
var pagesize = 4; //每页条数
var isOver = false; //是否加载完  

function getData() {
	mui.ajax(goods_url+'/goods/preferentialArea', {
		data: {
			"limit": pagesize,
			"page": pagenum,
			"shopType": "3",
			"sign": "string",
			"type_cid_partition": 3
		},
		dataType: 'json', //服务器返回json格式数据
		type: 'post', //HTTP请求类型
		timeout: 10000, //超时时间设置为10秒；
		headers: {
			'Content-Type': 'application/json'
		},
		success: function(data) {
			if(data.code == 10000) {
				//console.log(JSON.stringify(data.resp.length))
				var p = data.total / pagesize; //总页数
				if(pagenum >= p) { //当前的页数
					isOver = true;
					mui('#refreshContainer').pullRefresh().endPullupToRefresh(true);
					mui.toast("为您更新了商品" + data.resp.length + "条信息");
				}
				if(isOver == false) {
					pagenum++;
					mui('#refreshContainer').pullRefresh().endPullupToRefresh(true); //停止正在加载
					mui('#refreshContainer').pullRefresh().enablePullupToRefresh(); //显示上拉加载文字
					mui.toast("为您更新了商品" + data.resp.length + "条信息");
				}
				if(data.resp != "") {
					var html = "";
					for(var i = 0; i < data.resp.length; i++) {
						html += '<li class="mui-table-view-cell mui-media preferential" commodity_id=' + data.resp[i].id + '>';
						html += '<a href="javascript:;">';
						html += '<img class="mui-media-object mui-pull-left" src="'+data.resp[i].url+data.resp[i].image+'">';
						html += '<div class="mui-media-body">';
						html += '<div class="pre_listTit">'+data.resp[i].title+'</div>';
						html += '<div class="pre_listspan">';
						html += '<p>￥'+data.resp[i].price+'+';
						if(data.resp[i].special_of_integral ==null){
							html+= '0权益积分';
						}else{
							html += data.resp[i].special_of_integral+'权益积分';
						}
						html += '</p>';
						html += '';
						html += '<span>'+data.resp[i].sales_volume+'人付款</span>';
						html += '</div>';
						html += '<p>'+data.resp[i].business_name+'&nbsp;<span class="pre_listBtn">进店></span></p>';
						html += '</div>';
						html += '</a>';
						html += '</li>';
					}
					$("#my_list").append(html);
				} else {
					var html = "";
					$("#my_list").html('<div style=line-height:400px;text-align:center;color:#C0C0C0;font-size:16px;>暂无商品信息</div>');
				}
			} else {
				mui.alert(data.msg);
			}
		},
		error: function(xhr, type, errorThrown) { //异常处理；
			console.log(type);
			var error_msg = JSON.parse(xhr.responseText)
			mui.alert(error_msg.msg)
		}
	});
}
function pullupRefresh() {
	setTimeout(function() {
		mui('#refreshContainer').pullRefresh().disablePullupToRefresh(false);
		setTimeout(function() {
			getData();
			mui('#refreshContainer').pullRefresh().endPullupToRefresh((isOver)); //参数为true代表没有更多数据了。
		}, 500);
	}, 500);
}
//跳转商品详情
mui("body").on("tap", ".preferential", function() {
	var commodity_id = $(this).attr("commodity_id");
	//alert(commodity_id);
	mui.openWindow({
		url: "preferential_commodity_details.html",
		id: "commodity_details",
		extras: {
			commodity_id: commodity_id
		},
		createNew: true
	})
});
mui("body").on("tap", ".return", function() {
	mui.openWindow({
		url: '../../index.html'
	})
});
mui.back = function(){
	mui.openWindow({
		url: '../../index.html'
	})
}
mui("body").on("tap", "#preferential2", function() {
	window.top.location.href = 'preferential2.html'
});
mui("body").on("tap", "#preferential3", function() {
	window.top.location.href = 'preferential3.html'
});
mui("body").on("tap", "#preferential", function() {
	window.top.location.href = 'preferential.html'
});
//判断ios
var u = navigator.userAgent;
var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
if(isiOS) {
	$(".mui-content.gooding").css({"position":"inherit","padding-top":"242px",})
	$(".mui-content .gooding_scroll").css({"padding-bottom":"210px"})
	$("#my_list").css({"position":"relative",'top':'240px'})
}